﻿using System.Linq;
using MS.InformationManagement.BLL.DocIdGeneration;
using MS.InformationManagement.Presenter.Support;
using System;

namespace MS.InformationManagement.Presenter.DocumentIdConfig
{
    public class DocumentIdConfigPresenter : BasePresenter<IDocumentIdConfigView>
    {
        public DocumentIdConfigPresenter(IDocumentIdConfigView view) : base(view) { }

        public void HandleLoad()
        {
            if (View.DocumentTypeId.Equals(0))
            { LoadEmptyView(); }
            else { LoadDocumentTypeView(); }
        }

        private void LoadEmptyView()
        {
            View.DocumentTypeName = string.Empty;
            View.AvailableFields = DocIdStructureBLL.GetAllFields();
            View.Strategy = "Create";
            View.DocumentTypes = DocIdStructureBLL.GetRadTypes();
        }

        private void LoadDocumentTypeView()
        {
            View.AvailableFields = DocIdStructureBLL.GetAvailableFields(View.DocumentTypeId);
            View.ConfiguredFields = DocIdStructureBLL.GetRadType(View.DocumentTypeId);
            View.DocumentTypeName = View.ConfiguredFields.First().TipoDeRadicado.Title;
            View.DocumentTypeCode = View.ConfiguredFields.First().TipoDeRadicado.CódigoTipo;
            View.Strategy = "Update";
            View.DocumentTypes = DocIdStructureBLL.GetRadTypes();
        }

        public void DeleteDocType(string docTypeId) { 
            int convertedDocTypeId = Convert.ToInt32(docTypeId);
            DocIdStructureBLL.DeleteRadType(convertedDocTypeId);
        }

        public void RestartDocType(string docTypeId) {
            int convertedDocTypeId = Convert.ToInt32(docTypeId);
            DocIdStructureBLL.RestartRadTypeBank(convertedDocTypeId);
        }
    }
}
